package com.edu.weixin.service;

import java.io.IOException;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import com.edu.weixin.common.InputMessage;
import com.edu.weixin.common.OutputMessage;
import com.edu.weixin.util.SerializeXmlUtil;
import com.thoughtworks.xstream.XStream;

public abstract class AbstractMessage {

	public InputMessage getInputMessageForReq(HttpServletRequest request,XStream xs) throws IOException {
		ServletInputStream in = request.getInputStream();
		StringBuilder xmlMsg = new StringBuilder();
		byte[] b = new byte[4096];
		for (int n; (n = in.read(b)) != -1;) {
			xmlMsg.append(new String(b, 0, n, "UTF-8"));
		}
		return (InputMessage) xs.fromXML(xmlMsg.toString());
	}

	public XStream getXStreamForReq(HttpServletRequest request) throws IOException {
		XStream xs = SerializeXmlUtil.createXstream();
		xs.processAnnotations(InputMessage.class);
		xs.processAnnotations(OutputMessage.class);
		xs.alias("xml", InputMessage.class);
		return xs;
	}

}
